import axios from 'axios'
import { Message } from 'element-ui'
import store from '@/store'

const _axios = axios.create({
  baseURL: process.env.VUE_APP_URL,
  //   请求超时的时间5s
  timeout: 5000
})

_axios.interceptors.request.use(
  (config) => {
    // console.log('config:', config)
    if (store.state.detail.uuid_token) {
      config.headers.userTempId = store.state.detail.uuid_token
    }
    if (store.state.user.token) {
      config.headers.token = store.state.user.token
    }
    return config
  },
  (err) => {
    return Promise.reject(err)
  }
)
_axios.interceptors.response.use(
  (res) => {
    // console.log('res:', res)
    if (res.data.code === 200) {
      return res.data
    } else {
      Message.error(res.data.message)
      return Promise.reject()
    }
  },
  (err) => {
    return Promise.reject(err)
  }
)

export default _axios
